package com.deprecated.patch;

import com.panopset.centralen.UniqueKey;
import com.panopset.compat.Strings;
import com.panopset.sf.FontManager;
import com.panopset.sf.Windoc;

/**
 * User entry field.
 *
 * @author Karl Dinwiddie
 */
public class Field {

    private final boolean isPersistent;

    protected final Windoc wd;
    
    public final FontManager fm = FontManager.getInstance();

    private final String key;

    public Field(Windoc windoc, boolean persistent) {
        key = UniqueKey.generateStackHashKey();
        wd = windoc;
        isPersistent = persistent;
    }

    public Field(Windoc wd) {
        this(wd, true);
    }

    protected String translateTo(String s) {
        return s;
    }

    protected String translateFrom(String s) {
        return s;
    }

    /**
     * Is initialized.
     */
    private boolean isInitialized = false;
    /**
     * Gui.
     */
    private FieldGui gui;
    /**
     * Default.
     */
    private FieldDefault dft;
    /**
     * Value.
     */
    private FieldValue val;

    /**
     * @return Val value.
     */
    private String getValValue() {
        if (val == null) {
            return null;
        }
        if (isPersistent) {
            return translateFrom(val.getValue());
        }
        return "";
    }

    private String valueOnDeck;

    /**
     * @param s Value to set.
     */
    private void setValValue(final String s) {
        if (val == null) {
            valueOnDeck = s;
            return;
        }
        if (isPersistent) {
            val.setValue(translateTo(s));
        }
    }

    /**
     *
     * @return Value from FieldGui, FieldValue, or FieldDefault, in that order
     * of checking.  Empty String if nothing else has anything.
     */
    public final String getValue() {
    	if (!isPersistent) {
    		return gui.getValue();
    	}
        if (isInitialized
                && gui != null && gui.getValue() != null) {
            return gui.getValue();
        }
        if (val != null) {
            String s = getValValue();
            if (Strings.isPopulated(s)) {
                if (gui != null) {
                    gui.setValue(s);
                }
                isInitialized = true;
                return s;
            }
            if (gui != null) {
                gui.setValue("");
            }
        }
        if (dft != null) {
            if (val != null) {
                setValValue(dft.getValue());
            }
            if (gui != null) {
                gui.setValue(dft.getValue());
            }
            isInitialized = true;
            return dft.getValue();
        }
        isInitialized = true;
        return "";
    }

    /**
     * @param value Value.
     */
    public final void setValue(final String value) {
        setValValue(value);
        if (gui != null) {
            gui.setValue(value);
        }
    }

    /**
     * Persist using FieldDefault, if available.
     */
    public void save() {
        if (isPersistent) {
            getFieldValue().setValue(getValue());
        }
    }

    /**
     * @return Field gui.
     */
    public final FieldGui getFieldGui() {
        return gui;
    }

    /**
     * @param fieldGui Field gui.
     */
    public final void setFieldGui(final FieldGui fieldGui) {
        gui = fieldGui;
    }

    /**
     * @return Field default.
     */
    public final FieldDefault getFieldDefault() {
        return dft;
    }

    /**
     * @param fieldDefault Field default.
     */
    public final void setFieldDefault(final FieldDefault fieldDefault) {
        dft = fieldDefault;
    }

    /**
     * @return Field persistent value.
     */
    public final FieldValue getFieldValue() {
        return val;
    }

    /**
     * @param fieldValue Field value.
     */
    public final void setFieldValue(final FieldValue fieldValue) {
        val = fieldValue;
        if (valueOnDeck != null) {
            val.setValue(valueOnDeck);
            valueOnDeck = null;
        }
    }

    /**
     * Validation of field value.
     * @param valueToCheck Value to check.
     * @return true iff valid.
     */
    protected boolean isValid(final String valueToCheck) {
        return true;
    }

    /**
     * Reset to default.
     */
    public void resetToDefault() {
        if (dft != null) {
            setValue(dft.getValue());
        }
    }

    /**
     * Reset to default.
     */
    public void resetToFile() {
        if (val != null) {
            String s = val.getValue();
            if (Strings.isPopulated(s)) {
                setValue(val.getValue());
            }
        }
        resetToDefault();
    }
}
